﻿

#include "../include/Pipeline.h"
#include "../include/Predict.h"
#include <iostream>
#include <direct.h>
#include <stdio.h>
#include "cJSON.h"
#include <opencv2/imgcodecs/legacy/constants_c.h>

char* predict(jlong handle,char* img, int size) {

	cJSON  *root = cJSON_CreateObject(), *item = cJSON_CreateArray(),
	*cars = cJSON_CreateObject();
	char *resultJson;

	//char buffer[_MAX_PATH];
	//_getcwd(buffer, _MAX_PATH); 
	//std::string path = "";
	//path.append(buffer); 
	//pr::PipelinePR prc("E:/face/model/cascade.xml",
	//	"E:/face/model/HorizonalFinemapping.prototxt", "E:/face/model/HorizonalFinemapping.caffemodel",
	//	"E:/face/model/Segmentation.prototxt","E:/face/model/Segmentation.caffemodel",
	//	"E:/face/model/CharacterRecognization.prototxt","E:/face/model/CharacterRecognization.caffemodel",
	//	"E:/face/model/SegmenationFree-Inception.prototxt", "E:/face/model/SegmenationFree-Inception.caffemodel"
	//);
	pr::PipelinePR *PR = (pr::PipelinePR *) handle;
	/*cv::Mat img_decode;
	std::vector<uchar> data;
	for (int i = 0; i < size; ++i) {
		data.push_back(img[i]);
	}
	img_decode = cv::imdecode(data, CV_LOAD_IMAGE_COLOR);
	*/
	//清理内存
	//data.clear();
	//std::vector<uchar>(data).swap(data);

	//cv::imwrite("E:/face/1/lenna_gray.jpg", img_decode);
	cv::Mat img_decode = cv::imread("E:/face/1/1.jpg");

	std::vector<pr::PlateInfo> res = PR->RunPiplineAsImage(img_decode, pr::SEGMENTATION_FREE_METHOD);
	
	if (res.size() == 0) {
		cJSON_AddNumberToObject(root, "status", -1);
		cJSON_AddStringToObject(root, "msg", "error");
		cJSON_AddItemToObject(root, "data", item);
		resultJson = cJSON_PrintUnformatted(root);
		return resultJson;
	}
	cJSON_AddNumberToObject(root, "status", 0);
	cJSON_AddStringToObject(root, "msg", "success");
	cJSON_AddItemToObject(root, "data", item);
	for (auto st : res) {
		if (st.confidence > 0.8) {
			cJSON  *car = cJSON_CreateObject();
			cJSON_AddStringToObject(car, "carno", st.getPlateName().c_str());
			cJSON_AddStringToObject(car, "similar", std::to_string(st.confidence).c_str());
			cv::Rect region = st.getPlateRect();
			cJSON_AddStringToObject(car, "x", std::to_string(region.x).c_str());
			cJSON_AddStringToObject(car, "y", std::to_string(region.y).c_str());
			cJSON_AddStringToObject(car, "w", std::to_string(region.width).c_str());
			cJSON_AddStringToObject(car, "h", std::to_string(region.height).c_str());
			cJSON_AddItemToArray(item, car);
		}
	}

	img_decode.release();
	resultJson = cJSON_PrintUnformatted(root);
	cJSON_Delete(root);
	return resultJson;
}
/*
char* predictMat(cv::Mat img, int size, jlong handle) {

	cJSON  *root = cJSON_CreateObject(), *item = cJSON_CreateArray(),
		*cars = cJSON_CreateObject();
	char *resultJson;

	//char buffer[_MAX_PATH];
	//_getcwd(buffer, _MAX_PATH); 
	//std::string path = "";
	//path.append(buffer); 
	//pr::PipelinePR prc("E:/face/model/cascade.xml",
	//	"E:/face/model/HorizonalFinemapping.prototxt", "E:/face/model/HorizonalFinemapping.caffemodel",
	//	"E:/face/model/Segmentation.prototxt","E:/face/model/Segmentation.caffemodel",
	//	"E:/face/model/CharacterRecognization.prototxt","E:/face/model/CharacterRecognization.caffemodel",
	//	"E:/face/model/SegmenationFree-Inception.prototxt", "E:/face/model/SegmenationFree-Inception.caffemodel"
	//);
	pr::PipelinePR *PR = (pr::PipelinePR *) handle; 

	//cv::imwrite("E:/face/1/lenna_gray.jpg", img_decode);
	//cv::Mat image = cv::imread("E:/face/1/1.jpg"); 

	std::vector<pr::PlateInfo> res = PR->RunPiplineAsImage(img, pr::SEGMENTATION_FREE_METHOD);

	if (res.size() == 0) {
		cJSON_AddNumberToObject(root, "status", -1);
		cJSON_AddStringToObject(root, "msg", "there is no car");
		cJSON_AddItemToObject(root, "data", item);
		resultJson = cJSON_PrintUnformatted(root);
		return resultJson;
	}
	cJSON_AddNumberToObject(root, "status", 0);
	cJSON_AddStringToObject(root, "msg", "success");
	cJSON_AddItemToObject(root, "data", item);
	for (auto st : res) {
		if (st.confidence > 0.8) {
			cJSON  *car = cJSON_CreateObject();
			cJSON_AddStringToObject(car, "carno", st.getPlateName().c_str());
			cJSON_AddStringToObject(car, "similar", std::to_string(st.confidence).c_str());
			cv::Rect region = st.getPlateRect();
			cJSON_AddStringToObject(car, "x", std::to_string(region.x).c_str());
			cJSON_AddStringToObject(car, "y", std::to_string(region.y).c_str());
			cJSON_AddStringToObject(car, "w", std::to_string(region.width).c_str());
			cJSON_AddStringToObject(car, "h", std::to_string(region.height).c_str());
			cJSON_AddItemToArray(item, car);
		}
	} 
	img.release();
	resultJson = cJSON_PrintUnformatted(root);
	cJSON_Delete(root);
	return resultJson;
}
*/
jlong init(char* modelPath) {
	std::string path = modelPath;
	pr::PipelinePR *PR = new pr::PipelinePR(path + "/model/cascade.xml",
		path + "/model/HorizonalFinemapping.prototxt", path + "/model/HorizonalFinemapping.caffemodel",
		path + "/model/Segmentation.prototxt", path + "/model/Segmentation.caffemodel",
		path + "/model/CharacterRecognization.prototxt", path + "/model/CharacterRecognization.caffemodel",
		path + "/model/SegmenationFree-Inception.prototxt", path + "/model/SegmenationFree-Inception.caffemodel"
	);
	path.clear();
	std::string(path).swap(path);
	//std::cout << PR << std::endl;
	//printf("字符串的地址：%p\n", PR);
	return (jlong)PR;
}

void uninit(jlong handle) {
	pr::PipelinePR *PR = (pr::PipelinePR *) handle;
	delete PR;
	PR = NULL;
}

 